import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, BannerResult, GoodItem, HotGoods, Brand, HomeProduct, Special } from '@/types/data'
export default defineStore('home', {
  state() {
    return {
      // 轮播图广告的数据
      bannerList: [] as BannerResult[],
      // 新鲜好物的数据
      newGoodList: [] as GoodItem[],
      // 人气推荐数据
      hotGoodList: [] as HotGoods[],
      // 热门品牌数据
      brandList: [] as Brand[],
      // 商品推荐模块数据
      productList: [] as HomeProduct[],
      // 最新专题数据
      specialList: [] as Special[],
    }
  },
  actions: {
    // 获取轮播广告数据的方法
    async getBannerList() {
      const res = await request.get<ApiRes<BannerResult>>('/home/banner')
      this.bannerList = res.data.result

    },
    // 获取新鲜好物的数据
    async getNewGoodList() {
      // setTimeout(async () => {
      const res = await request.get<ApiRes<GoodItem>>('/home/new')
      this.newGoodList = res.data.result
      // }, 500)
    },
    // 获取人气推荐的数据
    async getHotList() {
      // setTimeout(async () => {
      const res = await request.get<ApiRes<HotGoods>>('/home/hot')
      console.log(res)
      this.hotGoodList = res.data.result
      // }, 500)
    },
    // 获取热门品牌数据
    async getBrandList() {
      // setTimeout(async () => {
      const res = await request.get<ApiRes<Brand>>('/home/brand')
      this.brandList = [...res.data.result, ...res.data.result]
      // }, 500)
    },
    // 获取商品推荐模块数据的方法
    async getProductList() {
      const res = await request.get<ApiRes<HomeProduct>>('/home/goods')
      this.productList = res.data.result
    },
    // 获取最新专题数据的方法
    async getSpecialList() {
      const res = await request.get<ApiRes<Special>>('/home/special')
      this.specialList = res.data.result
    },
  }
})